function loadWeather($this) {
	uni.getLocation({
		type: "gcj02",
		altitude: true,
		success: function(res) {
			console.log('latitude=', res.longitude)
			console.log('longitude=', res.longitude)
			// uni.showModal({
			// 	title: '如果你看到这个框，请截图发到群里，@1KG',
			// 	content: `get the gps\nlatitude=${res.latitude}\nlongitude=${res.longitude}`,
			// 	success: function(res) {}
			// });
			getCityName(res.latitude, res.longitude, $this).then(cityName => {
				console.log(cityName)
				console.log('city=', cityName)
				getWeatherInfo(cityName, $this)
			})
		}
	})
}

function getCityName(latitude, longitude, $this) {
	return new Promise(function(resolve, reject) {
		$this.$jsonp('https://apis.map.qq.com/ws/geocoder/v1/', {
			key: 'W75BZ-YCG6W-NKVRO-RJLLD-UWLS3-NPFI3',
			output: 'jsonp',
			location: `${latitude},${longitude}`
		}).then(res => {
			// alert('get location info from api=' + JSON.stringify(res))
			if (res.status !== 0) reject('fail')
			let city = res.result.ad_info.city
			console.log('city =', city)
			resolve(city)
		}).catch(err => {
			reject(err)
		})
	})
}

function getWeatherInfo(city, $this) {
	uni.request({
		url: `http://wthrcdn.etouch.cn/WeatherApi?city=${city}`,
		method: 'GET',
		success: res => {
			if (res.statusCode === 200) {
				const doc = new DOMParser().parseFromString(res.data, 'text/xml')
				// console.log(doc)
				let city = getXmlNodeValue(doc, 'city')
				let dayType = doc.documentElement.getElementsByTagName('forecast')[0]
					.childNodes[0].getElementsByTagName('day')[0]
					.childNodes[0].childNodes[0].data
				let wendu = getXmlNodeValue(doc, 'wendu')
				let fengxiang = getXmlNodeValue(doc, 'fengxiang')
				let fengli = getXmlNodeValue(doc, 'fengli')
				let sunrise_1 = getXmlNodeValue(doc, 'sunrise_1')
				let sunset_1 = getXmlNodeValue(doc, 'sunset_1')
				$this.$u.vuex('vuex_weather', {
					city: city,
					dayType: dayType,
					wendu: wendu,
					fengxiang: fengxiang,
					fengli: fengli,
					sunrise_1: sunrise_1,
					sunset_1: sunset_1
				})
			}
		},
		fail: () => {
			this.openmsg("警告", "天气接口获取失败")
		},
		complete: () => {}
	})
}

function getXmlNodeValue(doc, nodeName) {
	return doc.documentElement.getElementsByTagName(nodeName)[0].childNodes[0].data
}

module.exports = {
	loadWeather: loadWeather
}
